package com.incar.chuxing.utils.Dialogs;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.incar.chuxing.R;
import com.incar.chuxing.bean.AppConstants;
import com.incar.chuxing.utils.SPKey;
import com.incar.chuxing.utils.SPUtils;
import com.incar.chuxing.utils.SystemToastUtils;
import com.incar.chuxing.utils.ToastUtils;
import com.incar.chuxing.widget.WheelPicker;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

public class ChooseHoursDialog extends Dialog {
    Context mContext;
    private WheelPicker chooseDaysView;
    private TextView choose_cancel;
    private TextView choose_ok;
    private OnNoOnclickListener noOnclickListener;//取消按钮被点击了的监听器
    private OnYesOnclickListener yesOnclickListener;//确定按钮被点击了的监听器
    private int type;
    private TextView des;
    private List<AppConstants.DataBean.RentalLittleDaysOptionsBean> littleDaysOptionsBeans = new ArrayList<>();

    public ChooseHoursDialog(Context context, int type) {
        super(context, R.style.navi_dialog);
        this.mContext = context;
        this.type = type;
    }

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_choose_days);
        Window window = getWindow();
        window.setGravity(Gravity.BOTTOM);
        window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
        setCanceledOnTouchOutside(false);
        setCancelable(true);
        window.setWindowAnimations(R.style.dialog_choose_navi);
        initView();
        initData();
        initEvent();
    }

    /**
     * 设置取消按钮的显示内容和监听
     *
     * @param onNoOnclickListener
     */
    public void setNoOnclickListener(OnNoOnclickListener onNoOnclickListener) {

        this.noOnclickListener = onNoOnclickListener;
    }

    /**
     * 设置确定按钮的显示内容和监听
     *
     * @param onYesOnclickListener
     */
    public void setYesOnclickListener(OnYesOnclickListener onYesOnclickListener) {
        this.yesOnclickListener = onYesOnclickListener;
    }

    /**
     * 初始化界面控件
     */
    private void initView() {
        choose_cancel = (TextView) findViewById(R.id.choose_cancel);
        choose_ok = (TextView) findViewById(R.id.choose_ok);
        des = findViewById(R.id.desc);
        if (type == 1) {
            des.setVisibility(View.GONE);
        }

    }

    /**
     * 初始化界面的确定和取消监听器
     */
    private void initData() {

        chooseDaysView = findViewById(R.id.choose_days);
        //小日租时长设置

        littleDaysOptionsBeans = new Gson().fromJson(SPUtils.getInstance(mContext).getStringValue(SPKey.LITTLE_DAY_RENT_OPTIONS_DURATION, "[{\"id\":\"1\",\"text\":\"4小时\"},{\"id\":\"2\",\"text\":\"8小时\"},{\"id\":\"3\",\"text\":\"12小时\"}]"), new TypeToken<List<AppConstants.DataBean.RentalLittleDaysOptionsBean>>() {
        }.getType());
        AppConstants.DataBean.RentalLittleDaysOptionsBean bean=new AppConstants.DataBean.RentalLittleDaysOptionsBean();
        bean.setId("-1");
        bean.setText("请选择");
        littleDaysOptionsBeans.add(0,bean );
        chooseDaysView.setDataList(littleDaysOptionsBeans);
        chooseDaysView.setOnWheelChangeListener(new WheelPicker.OnWheelChangeListener<AppConstants.DataBean.RentalLittleDaysOptionsBean>() {
            @Override
            public void onWheelSelected(AppConstants.DataBean.RentalLittleDaysOptionsBean item, int position) {
                if (position==0){
                    choose_ok.setTextColor(getContext().getResources().getColor(R.color.black2));
                    choose_ok.setClickable(false);
                }else {
                    choose_ok.setClickable(true);
                    choose_ok.setTextColor(getContext().getResources().getColor(R.color.colorPrimary));
                }
            }
        });
    }

    /**
     * 初始化界面的确定和取消监听器
     */
    private void initEvent() {
        //设置确定按钮被点击后，向外界提供监听
        choose_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (yesOnclickListener != null) {
                    if (chooseDaysView.getCurrentPosition() == 0) {
                        SystemToastUtils.makeText(mContext, "请选择用车时长", ToastUtils.LENGTH_LONG).show();
                    } else {
                        yesOnclickListener.onYesClick(chooseDaysView.getCurrentPositionId(),chooseDaysView.getCurrentPositionText());

                    }
                }
            }
        });
        //设置取消按钮被点击后，向外界提供监听
        choose_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (noOnclickListener != null) {
                    noOnclickListener.onNoClick();
                }
            }
        });
    }

    /**
     * 设置确定按钮和取消被点击的接口
     */
    public interface OnYesOnclickListener {
        public void onYesClick(String selectedId,String selectedText);
    }

    public interface OnNoOnclickListener {
        public void onNoClick();
    }


}